var files;
var photoAlbum;
var selected_photo_id;
var swfu;

window.onload = function () {
	swfu = new SWFUpload({
		// Backend Settings
		upload_url: 'index.php/photosuploader/uploadPhotoSWF',
		flash_url : "../swfupload_f9.swf",
		// Don't think this is needed
		post_params: {"PHPSESSID": "<?php echo session_id(); ?>"},

		// File Upload Settings
		file_size_limit : "2048",    // 2MB
		file_types : "*.jpg",
		file_types_description : "JPG Images",
		file_upload_limit : "0",

		// Event Handler Settings - these functions as defined in Handlers.js
		//  The handlers are not part of SWFUpload but are part of my website and control how
		//  my website reacts to the SWFUpload events.
		file_queue_error_handler : fileQueueError,
		file_dialog_complete_handler : fileDialogComplete,
		upload_progress_handler : uploadProgress,
		upload_error_handler : uploadError,
		upload_success_handler : uploadSuccess,
		upload_complete_handler : uploadComplete,

		custom_settings : {
			upload_target : "divFileProgressContainer"
		},

		// Debug Settings
		debug: false
	});
	formLoadMap(); 
}

$(document).ready(
	function () {
		$('div').Sortable(
			{
				accept : 		'sortableitem',
				helperclass : 	'sorthelper',
				activeclass : 	'sortableactive',
				hoverclass : 	'sortablehover',
				opacity: 		0.8,
				/*fx:				200,*/
				revert:			true,
				floats:			true,
				tolerance:		'pointer'
			}
		)
	}
);


function selectFiles(){
	if(id_note != -1){
		swfu.addPostParam('idnotejm',id_note);
		swfu.selectFiles();
		this.blur();
		$('#reloadAlbumButton').click(function(){reloadPhotoAlbum(id_note);});
		reloadPhotoAlbum(id_note);
	}
	else{
		alert("Save your note before adding photos");
	}
}

function orderPhoto(id){
	serial = $.SortSerialize('sortable1');
	$.ajax({
		type: "POST",
		url: "photosuploader/orderPhoto",
		data: "imageId="+id+"&order="+serial,
		success: function(){
		}
	});
}



function callUploadPhoto(imageName){
			$.ajax({
				type: "POST",
				url: "photosuploader/uploadPhoto",
				dataType: "xml",
				data: "imageName="+imageName,
				success: function(xml){
				}
			});
}

function reloadPhotoAlbum(id_note){
	/*first a get with photos from the current note*/
	//if(id_note!=-1){
		$.ajax({
			type: "POST",
			url: "photosuploader/photoAlbum",
			data : "idNote="+id_note,
			dataType: "json",
			success : function(json){
				var photos_directory = json['photos_directory'];
				var images = '';
				var photoAlbumLength = (json['photoAlbum']).length;
				for (var i = 0; i < (json['photoAlbum']).length ; i++) {
					//images += '<span id=\'sortable'+i+'\' class=\"sortableitem\" onmouseup="orderPhoto('+json['photoAlbum'][i].id+')">';
					images += '<img src =\''+photos_directory+'squares100/'+json['photoAlbum'][i].filename+'\' onclick=\'selectPhoto('+json['photoAlbum'][i].id+',\"'+photos_directory+'small/'+json['photoAlbum'][i].filename+'\")\'  >';
					//images += '</span>';
				}
				$("#photoAlbum").html(images);
				/*then the script for moving thumbnails*/
				$(document).ready(
					function(){
						$('div').Sortable(
							{
								accept : 		'sortableitem',
								helperclass : 	'sorthelper',
								activeclass : 	'sortableactive',
								hoverclass : 	'sortablehover',
								opacity: 		0.8,
								revert:			true,
								floats:			true,
								tolerance:		'pointer'
							}
						)
					}
				);
			}
		});

	
}
function selectPhoto(i,u){
	selected_photo_id = i;
	document.getElementById("seeSelectedPhoto").src = ""+u;
	document.getElementById("selectedPhoto").style.visibility = "visible";
	document.getElementById("selectedPhoto").style.width = "420";
}


function closeSeePhoto(){
	document.getElementById("seeSelectedPhoto").src = "";
	document.getElementById("selectedPhoto").style.visibility = "hidden";
}
function deletePhoto(){
	//alert(selected_photo_id);
	$.ajax({
				type: "POST",
				url: "photosuploader/deletePhoto",
				data: "imageId="+selected_photo_id,
				success: function(){
					reloadPhotoAlbum();
				}
	});
	selected_photo_id = '';
	document.getElementById("seeSelectedPhoto").src = "";
	document.getElementById("selectedPhoto").style.visibility = "hidden";
}